#include "math_3d.h"
#define GLEW_STATIC 1
#include <GL/glew.h>
#include <glut.h>

#define RUN_TESTS 1
GLuint VBO;

static void RenderSceneCB()
{
	printf("Rendering ..\n");
    glClear(GL_COLOR_BUFFER_BIT);
	glPointSize(4.0);
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);

    glDrawArrays(GL_TRIANGLES, 0, 3);
	//glDrawArrays(GL_POINTS, 0, 3);
	//glDrawArrays(GL_LINES, 0, 4);

    glDisableVertexAttribArray(0);
    glutSwapBuffers();
}

void TimerFunct(int param)
{
	
	glutTimerFunc(100,TimerFunct,1);
	 glutPostRedisplay();
}

static void InitializeGlutCallbacks()
{
    glutDisplayFunc(RenderSceneCB);
	glutTimerFunc(100,TimerFunct,1);
}

static void CreateVertexBuffer()
{
    Vector3f Vertices[4];
    Vertices[0] = Vector3f(-1.0f, -0.0f, 0.0f);
	Vertices[1] = Vector3f(1.0f, 0.0f, 0.0f);
    Vertices[2] = Vector3f(0.0f, -1.0f, 0.0f);
	Vertices[3] = Vector3f(0.0f, 1.0f, 0.0f);

	float verts[] = 
	{
			+0.0f, +0.1f,
			-0.1f, -0.1f,
			+0.1f, -0.1f,
	};
    //Vertices[2] = Vector3f(0.0f, 1.0f, 0.0f);

    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
}

int main_app(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
    glutInitWindowSize(1024, 768);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Tutorial 03");

    InitializeGlutCallbacks();

    // Must be done after glut is initialized!
    GLenum res = glewInit();
    if (res != GLEW_OK) {
      fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
      return 1;
    }

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    CreateVertexBuffer();

    glutMainLoop();

    return 0;
}

#include <gtest/gtest.h>
int main(int argc, char **argv)
{
#if defined RUN_TESTS && RUN_TESTS > 0
	::testing::InitGoogleTest(&argc, argv);
	RUN_ALL_TESTS();
	printf("Press Enter to continue...\n");
	getchar();
#endif
	main_app(argc,argv);

}